package com.pure.controller;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pure.common.ReturnData;
import com.pure.common.SysCodeMsg;
import com.pure.db.Collect;
import com.pure.db.Product;
import com.pure.db.User;
import com.pure.service.MallService;

@Controller
public class MallController extends BaseController{
	@Resource
	private MallService mallService;
	private  static final String uploadPath="/upload";
	
	@RequestMapping(value = "addProduct", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData addProduct(Product product) {
		ReturnData returnData = new ReturnData();
		try {
			if (product == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			Integer count = mallService.addProduct(product);
			returnData.setData(count);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "getProductByUserId", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData getProductByUserId(@RequestParam Integer userId) {
		ReturnData returnData = new ReturnData();
		try {
			if (userId == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
		    List<Product> result = mallService.getProductByUserId(userId);
			returnData.setData(result);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "searchProduct", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData searchProduct(@RequestParam String productname) {
		ReturnData returnData = new ReturnData();
		try {
			
			List<Product> result = mallService.searchProduct(productname);
			returnData.setData(result);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "searchProductAndSeller", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData searchProductAndSeller(@RequestParam String productname) {
		ReturnData returnData = new ReturnData();
		try {
			
			List<Map<String, Object>> result = mallService.searchProductAndSeller(productname);
			returnData.setData(result);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "getProductById", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData getProductById(@RequestParam Integer id) {
		ReturnData returnData = new ReturnData();
		try {
			if (id == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			Product result = mallService.getProductById(id);
			returnData.setData(result);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "updateProduct", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData updateProduct(Product product) {
		ReturnData returnData = new ReturnData();
		try {
			if (product == null ) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			if (product.getId() == null ) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			Integer count = mallService.updateProduct(product);
			returnData.setData(count);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "deleteProduct", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData deleteProduct(Product product) {
		ReturnData returnData = new ReturnData();
		try {
			if (product == null || product.getId() == null ) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			
			Integer count = mallService.deleteProduct(product.getId());
			returnData.setData(count);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "addCollect", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData addCollect(Collect collect) {
		ReturnData returnData = new ReturnData();
		try {
			if (collect == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			
			Integer count = mallService.addCollect(collect);
			returnData.setData(count);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "getUsrCollect", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData getUsrCollect(@RequestParam Integer userid) {
		ReturnData returnData = new ReturnData();
		try {
			if (userid == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			
			List<Map<String, Object>> result = mallService.getUsrCollect(userid);
			returnData.setData(result);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "cancelCollect", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData cancelCollect(Collect collect) {
		ReturnData returnData = new ReturnData();
		try {
			if (collect == null || collect.getId() == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			
			Integer count = mallService.cancelCollect(collect.getId());
			returnData.setData(count);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "getProductCollect", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData getProductCollect(@RequestParam Integer productid) {
		ReturnData returnData = new ReturnData();
		try {
			if (productid == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			
			Map<String, Object> map = mallService.getProductCollect(productid);
			returnData.setData(map);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	@RequestMapping(value = "getProductCollectUserInfo", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public ReturnData getProductCollectUserInfo(@RequestParam Integer productid) {
		ReturnData returnData = new ReturnData();
		try {
			if (productid == null) {
				returnData.setCode(SysCodeMsg.CODE_10007);
				returnData.setMessage(SysCodeMsg.MSG_10007);
			}
			
			List<User> map = mallService.getProductCollectUserInfo(productid);
			returnData.setData(map);
			return returnData;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnData.setCode(SysCodeMsg.CODE_10000);
			returnData.setMessage(SysCodeMsg.MSG_10000);
		}
		return returnData;
	}
	
	
	@RequestMapping(value="upload", method=RequestMethod.POST)
	@ResponseBody
    public ReturnData handleFileUpload(HttpServletRequest request,HttpServletResponse response,
    		 @RequestParam("file") MultipartFile file){
		ReturnData returnData = new ReturnData();
		String savePath="";
        if (!file.isEmpty()) {
            try {
            	
            	//获取文件上传需要保存的路径，upload文件夹需存在。  
                String path = request.getSession().getServletContext().getRealPath("/");
                path=path+"/../"+uploadPath;
                savePath=uploadPath;
                
                File upload=new File(path);
                if (!upload.isDirectory()) {
                	upload.mkdirs();
        		}
                String fileName=file.getOriginalFilename();
               // String filePath = path+"/"+fileName;
                //生成随机文件名
                String type=fileName.substring(fileName.indexOf("."));
                UUID uuid = UUID.randomUUID();
                String saveName=uuid.toString().replace("-", "")+type;
                String filePath=path+"/"+saveName;
                //返回保存的全路径
                savePath=savePath+"/"+saveName;
                File fFile=new File(filePath);
                //返回保存的全路径
                //savePath=savePath+"/"+fileName;
                //复制文件
                file.transferTo(fFile);
                
            } catch (Exception e) {
                e.printStackTrace();
                returnData.setCode(SysCodeMsg.CODE_10000);
    			returnData.setMessage(SysCodeMsg.MSG_10000);
                return returnData;
            }
        }
        returnData.setData(savePath);
        return returnData;
    }
}
